package com.weiyu.bultrasound.core.model.vo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.weiyu.bultrasound.core.model.common.GenderEnum;
import com.weiyu.bultrasound.core.model.common.IdCategoryEnum;
import com.weiyu.bultrasound.core.model.entity.Files;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *@description 申请单视图实体
 *@author abner chow
 *@date 2019-08-09 11:22
 */
@Data
public class ApplicationsVO implements Serializable {

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value="主键id")
    private Long id;

    @ApiModelProperty(value="申请代码")
    private String applyCode;

    @ApiModelProperty(value="患者id")
    private String patientId;

    @ApiModelProperty(value="患者名字")
    private String patientName;

    @ApiModelProperty(value="性别代码")
    private String genderCode;

    @ApiModelProperty(value="性别")
    private String gender;

    @ApiModelProperty(value="年龄")
    private Integer age;

    @ApiModelProperty(value="证件类型代码")
    private String birthday;

    @ApiModelProperty(value="证件类型代码")
    private String idCategoryCode;

    @ApiModelProperty(value="证件类型")
    private String idCategory;

    @ApiModelProperty(value="证件号码")
    private String idNo;

    @ApiModelProperty(value="病房床位号")
    private String positionCode;

    @ApiModelProperty(value="手持超声序列号")
    private String handultSn;

    @ApiModelProperty(value="业务名称代码")
    private String busCode;

    @ApiModelProperty(value="申请时间")
    private Date applyDate;

    @ApiModelProperty(value="B超检测开始时间")
    private Date beginTime;

    @ApiModelProperty(value="状态 1. 未检测 2. 未上传（APP状态，后台列表无此状态）3. 已上传 4. 已同步")
    private Integer status;

    @ApiModelProperty(value="申请单的文件列表")
    private List<Files> filesList;

    public void setGenderCode(String genderCode) {
        this.genderCode = genderCode;
        setGender(genderCode);
    }

    public void setGender(String genderCode) {
        String gender = "";
        if(StringUtils.hasText(genderCode)){
            gender = GenderEnum.getByCode(genderCode);
        }else {
            gender = GenderEnum.getByCode(this.genderCode);
        }
        this.gender = gender;
    }

    public void setIdCategoryCode(String idCategoryCode) {
        this.idCategoryCode = idCategoryCode;
        setIdCategory(idCategoryCode);
    }

    public void setIdCategory(String idCategoryCode) {
        String idCategory = "";
        if(StringUtils.hasText(idCategoryCode)){
            idCategory = IdCategoryEnum.getByCode(idCategoryCode);
        }else {
            idCategory = IdCategoryEnum.getByCode(this.idCategoryCode);
        }
        this.idCategory = idCategory;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
        setAge();
        setBirthday();
    }

    public void setAge() {
        Integer age = null;
        //证件类型需是身份证
        if( this.idCategoryCode.equals(IdCategoryEnum.IDENTITY_CARD.getCode()) && StringUtils.hasText(this.idNo)) {
            Integer birthdayYear = Integer.parseInt(this.getIdNo().substring(6, 10));
            Integer birthdayMonth = Integer.parseInt(this.getIdNo().substring(10, 12));
            Integer birthdayDay = Integer.parseInt(this.getIdNo().substring(12, 14));
            age = getAgeByBirth(birthdayYear, birthdayMonth, birthdayDay);
        }
        this.age = age;
    }

    public void setBirthday() {
        String birthday= "";
        if( this.idCategoryCode.equals(IdCategoryEnum.IDENTITY_CARD.getCode()) && StringUtils.hasText(this.idNo)) {
            Integer birthdayYear = Integer.parseInt(this.getIdNo().substring(6, 10));
            Integer birthdayMonth = Integer.parseInt(this.getIdNo().substring(10, 12));
            Integer birthdayDay = Integer.parseInt(this.getIdNo().substring(12, 14));
            birthday = birthdayYear.toString()+"-"+birthdayMonth.toString()+"-"+birthdayDay.toString();
        }
        this.birthday = birthday;
    }

    //计算年龄
    public static int getAgeByBirth(Integer yearBirth , Integer monthBirth , Integer dayOfMonthBirth ) {
        int age = 0;
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
        age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前，年龄减一
            } else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        return age;
    }


}
